package com.intellagric.agriecom.service.agriecom_merchant;


import com.github.pagehelper.PageHelper;
import com.intellagric.agriecom.module.agriecom_merchant.MerchantService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_merchant.AgriecomMerchantDao;
import com.intellagric.mapper.AgriecomMerchantMapper;
import com.intellagric.pojo.AgriecomMerchant;
import com.intellagric.pojo.AgriecomMerchantExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private AgriecomMerchantMapper agriecomMerchantMapper;

    @Autowired
    private AgriecomMerchantDao agriecomMerchantDao;

    @Override
    public ResponseMessage addMerchant(AgriecomMerchant agriecomMerchant) {
        if (agriecomMerchantMapper.insertSelective(agriecomMerchant) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }


    @Override
    public ResponseMessage deleteMerchant(String id) {
        if (agriecomMerchantMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }


    @Override
    public ResponseMessage editMerchant(AgriecomMerchant agriecomMerchant) {
        agriecomMerchant.setUpdateDate(new Date());

        if (agriecomMerchantMapper.updateByPrimaryKeySelective(agriecomMerchant) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public AgriecomMerchant getMerchantById(String id) {
        return agriecomMerchantMapper.selectByPrimaryKey(id);
    }



    @Override
    public LayuiDataGridResult getMerchantList() {
        AgriecomMerchantExample agriecomMerchantExample=new AgriecomMerchantExample();
        agriecomMerchantExample.createCriteria().andMerchantIdIsNotNull();
        List<AgriecomMerchant> merchantList=agriecomMerchantMapper.selectByExampleWithBLOBs(agriecomMerchantExample);

        return LayuiDataGridResult.success().add(merchantList,merchantList.size());
    }

    /**
     * 增加了关键词搜索
     * @param page
     * @param rows
     * @param keyword
     * @return
     */
    @Override
    public LayuiDataGridResult getMerchantList(int page, int rows,String keyword) {
        PageHelper.startPage(page, rows);

        AgriecomMerchantExample agriecomMerchantExample=new AgriecomMerchantExample();
        if(keyword!=null&&keyword!="")
            agriecomMerchantExample.createCriteria().andMerchantNameLike("%"+keyword+"%");
        else
            agriecomMerchantExample.createCriteria().andMerchantIdIsNotNull();
        List<AgriecomMerchant> merchantList=agriecomMerchantMapper.selectByExampleWithBLOBs(agriecomMerchantExample);

        return LayuiDataGridResult.success().add(merchantList, merchantList.size());

    }


    @Override
    public ResponseMessage batchDeleteMerchant(String[] ids) {


        if (agriecomMerchantDao.batchDeleteByPrimaryKey(ids) <1)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }




}
